﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using SouthernLand.Common;
using SouthernLand.Common.BOL.Articles;
using SouthernLand.Common.Untilities;

namespace SouthernLand.Articles
{
    public partial class Default : BasePage
    {
        private int CateID = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            CateID = Globals.GetIntFromQueryString("CateID");
            if (CateID > 0)
            {
                FrameTitle.Text = ArticleCategory.GetCateNameByCateID(CateID);

                Pager.IndexChanged += PageIndex_Changed;
                if (!Page.IsPostBack)
                    DataBind_Article();

                Load_Title(CateID);
            }
            else
                CateID = ConvertUtility.ToInt32(EnumCategory.TinTucOnline);
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            rptArticles.ItemDataBound += rptArticles_ItemDataBound;
        }

        private void rptArticles_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var article = (Article) e.Item.DataItem;
                if (article != null)
                {
                    var hplNewsTitle = (HyperLink) e.Item.FindControl("hplNewsTitle");
                    hplNewsTitle.Text = article.Title;
                    hplNewsTitle.NavigateUrl = Globals.BuildUrlByNews(article.ArticleID,
                                                                      Globals.ProcessString(
                                                                          Globals.UnicodeToKoDau(article.Title)));
                    HyperLink hplReadMore = (HyperLink) e.Item.FindControl("hplReadMore");
                    hplReadMore.NavigateUrl = Globals.BuildUrlByNews(article.ArticleID,
                                                                      Globals.ProcessString(
                                                                          Globals.UnicodeToKoDau(article.Title)));
                    hplReadMore.Text = ResourceManager.GetString("ReadMore");
                }
            }
        }

        private void PageIndex_Changed(Object sender, EventArgs e)
        {
            DataBind_Article();
        }

        public void DataBind_Article()
        {
            Pager.PageSize = 15;
            List<Article> lSearch = Article.Search(CateID, "", "", Pager.PageIndex, Pager.PageSize);
            rptArticles.DataSource = lSearch;
            rptArticles.DataBind();

            Pager.TotalRecords = CurrentPage.TotalRecords = Article.SearchCount(CateID, "");
            CurrentPage.TotalPages = Pager.CalculateTotalPages();
            CurrentPage.PageIndex = Pager.PageIndex;
            DisplayPager.Visible = Pager.TotalRecords > Pager.PageSize;
        }

        private void Load_Title(int CateID)
        {
            if (CateID > 0)
            {
                // kiem tra xem can hien thi san pham hinh thuc nao
                List<ArticleCategory> obj = ArticleCategory.GetArticleCategoryByParentID(CateID);
                // xem loai cha
                if (obj.Count > 0)
                {
                    FrameTitle.Text = ArticleCategory.GetCateNameByCateID(CateID);
                    ArticlesByParent.Visible = true;
                    PanelArticles.Visible = false;
                }
                else
                {
                    ArticleCategory cate = ArticleCategory.GetArticleCategoryByID(CateID);
                    FrameTitle.Text = ArticleCategory.GetCateNameByCateID(cate.ParentID) + " <img src=" +
                                      Globals.ImagesUrl() + "li1.gif align='absmiddle'> " +
                                      ArticleCategory.GetCateNameByCateID(CateID);

                    ArticlesByParent.Visible = false;
                    PanelArticles.Visible = true;
                }
            }
            else
            {
                FrameTitle.Text = Globals.GetLang("Tin tức", "News");
            }
        }
    }
}